package com.jqb.servletlab.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class QueryServlet extends HttpServlet {
	private static final long serialVersionUID = 586313258473463870L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = null;
		String queryStr = request.getQueryString();
		if (queryStr != null) {
			out = response.getWriter();
			String[] msgs = queryStr.split("&");
			System.out.println(msgs[0]);
			for (String s : msgs) {
				String[] map = s.split("=");
				out.println(map[0] + "=" + map[1]);
			}
			out.close();
		} else {
			response.sendRedirect("/test/errorPage");
		}
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

}
